#include "stdafx.h"
#include "RSRegister.h"

RSRegister::RSRegister(void)
{
	hKey = NULL;
	ErrorCode = 0;
}

RSRegister::~RSRegister(void)
{
	CloseKey();
	ErrorCode = 0;
}

void RSRegister::CloseKey()
{
	if (hKey)
	{
		RegCloseKey(hKey);
		hKey = NULL;
	};
	ErrorCode = 0;
};

bool RSRegister::OpenKey(std::wstring Key)
{
	CloseKey();
	ErrorCode = RegOpenKeyEx( HKEY_CURRENT_USER, Key.data(), 0, KEY_QUERY_VALUE, &hKey );
	if( ErrorCode == ERROR_SUCCESS )
		return true;
	else
		return false;	
};

DWORD RSRegister::GetInteger(std::wstring Key)
{
	DWORD ret = 0;
	if (!hKey)
	{
		ErrorCode = -1;
		return ret;
	};
		
	DWORD sz;
	ErrorCode = RegQueryValueEx(hKey, Key.data(), NULL, NULL, (BYTE*) &ret, &sz);
	return ret;
};

std::wstring RSRegister::GetWString(std::wstring Key)
{
	std::wstring ret = TEXT("");
	if (!hKey)
	{
		ErrorCode = -1;
		return ret;
	};
		
	TCHAR *szProductType;
	DWORD dwBufLen = 0;
	ErrorCode = RegQueryValueEx( hKey, Key.data(), NULL, NULL, NULL, &dwBufLen);
	if (ErrorCode == ERROR_SUCCESS)
	{
		szProductType =new TCHAR[dwBufLen + 1];
		ErrorCode = RegQueryValueEx( hKey, Key.data(), NULL, NULL, (LPBYTE)szProductType, &dwBufLen);
		if (ErrorCode == ERROR_SUCCESS)
			ret = szProductType;
		delete szProductType;
		szProductType = NULL;
	};

	return ret;
};

LONG RSRegister::LastError()
{
	return ErrorCode;
};